package from_0_to_50;

import javax.security.auth.login.CredentialNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;

public class Q034searchRange {
    static ArrayList<ArrayList<Integer>> list;
    public static void main(String[] args) {
        int[] a = new int[]{5,7,7,8,8,10};
        System.out.println("searchRange(a) = " + searchRange(a,8));
    }


    public static int[] searchRange(int[] nums, int target) {
        list = new ArrayList<>();

        int[] a = new int[]{-1,-1};
        int left = 0, right = nums.length-1;
        int index = 0;
        while (left <= right){
            if(nums[(left + right)/2] > target){
                right = (left + right)/2-1;
            }else if(nums[(left + right)/2] < target) {
                left = (left + right)/2+1;
            }else {
                a[0] = a[1] = (left + right)/2;
                break;
            }
            System.out.println("Arrays.toString(a) = " + Arrays.toString(a));
            System.out.println((left + right)/2);
            System.out.println("left = " + left);
            System.out.println("right = " + right);
        }
        System.out.println("Arrays.toString(a) = " + Arrays.toString(a));
        if(a[0] != -1){
            for (int i = a[1]; i < nums.length-1 ; i++) {
                if(nums[i+1] == target)
                    a[1] += 1;

            }
            for (int i = a[0]; i > 0 ; i--) {
                if(nums[i-1] == target)
                    a[0] -= 1;
            }
        }
        System.out.println("Arrays.toString(a) = " + Arrays.toString(a));


        return a;
    }
}
